package com.baselibrary.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import com.baselibrary.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 关忆北丶
 * @description:
 * @date :2021/6/2 16:37
 */
public class ColorTextUtils {
    /**
     * @param context
     * @param content 整体内容
     * @param tagStr  需要变化的内容
     * @return
     */
    public static SpannableString setKeyWordColor(Context context, String content, String tagStr) {
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(tagStr)) {
            return new SpannableString("");
        }

        if (content.contains(tagStr) || content.trim().equals(tagStr.trim())) {
            SpannableString spannableString = new SpannableString(content);
            Pattern p = Pattern.compile(tagStr);
            Matcher m = p.matcher(spannableString);

            while (m.find()) {
                int start = m.start();
                int end = m.end();
                spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_F54D53)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return spannableString;
        } else {
            return new SpannableString(content);
        }
    }

    /**
     * @param content 整体内容
     * @param tagStr  需要变化的内容
     * @return
     */
    public static SpannableString setKeyWordColor2(String content, String tagStr) {
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(tagStr)) {
            return new SpannableString("");
        }

        if (content.contains(tagStr) || content.trim().equals(tagStr.trim())) {
            SpannableString spannableString = new SpannableString(content);
            Pattern p = Pattern.compile(tagStr);
            Matcher m = p.matcher(spannableString);

            while (m.find()) {
                int start = m.start();
                int end = m.end();
                spannableString.setSpan(new AbsoluteSizeSpan(12, true), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#333333")), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new StyleSpan(Typeface.BOLD), 0, content.length() - tagStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new StyleSpan(Typeface.NORMAL), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return spannableString;
        } else {
            return new SpannableString(content);
        }
    }

    public static SpannableString setKeyWordColor(Context context, String content, String tagStr, int tvColor) {
        if (TextUtils.isEmpty(content) || TextUtils.isEmpty(tagStr)) {
            return new SpannableString("");
        }

        if (content.contains(tagStr) || content.trim().equals(tagStr.trim())) {
            SpannableString spannableString = new SpannableString(content);
            Pattern p = Pattern.compile(tagStr);
            Matcher m = p.matcher(spannableString);

            while (m.find()) {
                int start = m.start();
                int end = m.end();
                spannableString.setSpan(new ForegroundColorSpan(context.getResources().getColor(tvColor)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            return spannableString;
        } else {
            return new SpannableString(content);
        }
    }

    /**
     * 参数：maxLines 要限制的最大行数
     * 参数：content  指TextView中要显示的内容
     */
    public static void setMaxEcplise(Context context, final TextView mTextView, final int maxLines, final String content, String tagStr) {

        ViewTreeObserver observer = mTextView.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                SpannableString spannableString = ColorTextUtils.setKeyWordColor(context, content, tagStr);
                mTextView.setText(spannableString);
                if (mTextView.getLineCount() > maxLines) {
                    int lineEndIndex = mTextView.getLayout().getLineEnd(maxLines - 1);
                    //下面这句代码中：我在项目中用数字3发现效果不好，改成1了
                    String text = content.subSequence(0, lineEndIndex - 3) + "...";
                    SpannableString spannableString2 = ColorTextUtils.setKeyWordColor(context, text, tagStr);
                    mTextView.setText(spannableString2);
                } else {
                    removeGlobalOnLayoutListener(mTextView.getViewTreeObserver(), this);
                }
            }
        });
    }


    public static String substringDes(String des, int digit) {
        if (des.length() > digit) {
            des = des.substring(0, digit) + "...";
        }
        return des;
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private static void removeGlobalOnLayoutListener(ViewTreeObserver obs, ViewTreeObserver.OnGlobalLayoutListener listener) {
        if (obs == null)
            return;
        if (Build.VERSION.SDK_INT < 16) {
            obs.removeGlobalOnLayoutListener(listener);
        } else {
            obs.removeOnGlobalLayoutListener(listener);
        }
    }


}
